﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.product
{
    public partial class CardType : BaseAdmin
    {

        readonly DALCardType theCardType = new DALCardType();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(ERule.CardType);
                this.BindGridView();
            }
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
          
            tbCardType tempCardType = theCardType.GetModelByName(this.txtCardTypeName.Text.Trim());
          
            if (tempCardType != null)
            {
                ExtAspNet.Alert.Show("该礼品卡类别已存在,请重新输入礼品卡类别！");
                return;
            }
            tbCardType oCardType = new tbCardType();
            oCardType.Name = this.txtCardTypeName.Text.Trim();
            oCardType.Description = this.txtCardTypeDescription.Text.Trim();
            oCardType.AddTime = DateTime.Now;
            theCardType.Add(oCardType);
            ExtAspNet.Alert.Show("添加成功！");
            this.txtCardTypeName.Text = "";
            this.txtCardTypeDescription.Text = "";
            this.BindGridView();
        }


        void BindGridView()
        {

            DataTable table = GetDataTable();
            this.Grid1.DataSource = table;
            this.Grid1.DataBind();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGridView();
        }

        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "deleted")
            {
                object[] keys = Grid1.DataKeys[e.RowIndex];

                string id = keys[0].ToString();
                if (!theCardType.IsDelete(id))
                {
                    ExtAspNet.Alert.Show("卡片类别已被使用,无法删除！");
                    return;
                }
                else
                {
                    theCardType.Delete(int.Parse(id));
                    ExtAspNet.Alert.Show("删除成功！");
                    this.BindGridView();
                }
            }
        }

        DataTable GetDataTable()
        {
            DataSet ds;
            string sWhere = "";
            if (this.txtKeywords.Text.Trim() != "")
            {
                sWhere = string.Format(" name like '%{0}%'", this.txtKeywords.Text.Trim());
            }
            ds = theCardType.GetList(sWhere);
            DataTable table = ds.Tables[0];
            return table;
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = GetDataTable();

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }






    }
}